package com.cn.mobileproject.view.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.mobileproject.R;
import com.cn.mobileproject.activity.CreateTaskActivity;
import com.cn.mobileproject.bean.Problem;
import com.cn.mobileproject.utils.TextHelper;

public class ProblemAdapter extends BaseAdapter {

	private static final String TAG = "ProblemAdapter";
	private List<Problem> list = null;
	private Context mContext;
	
	public ProblemAdapter(Context context){
		mContext = context;
	}
	
	public List<Problem> getList() {
		return list;
	}

	public void setList(List<Problem> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		if (list == null)
			return 0;
		int size = list.size();
		Log.d(TAG, "size=" + size);
		return size;
	}

	@Override
	public Object getItem(int position) {
		if (list == null)
			return null;
		if (position < list.size()) {
			return list.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	class ViewHolder {
		TextView problemText;
		TextView problemTypeText;
		TextView typeText;
		TextView requieText;
		LinearLayout problemLayout;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.problem_item, null);
			holder = new ViewHolder();
			holder.problemLayout = (LinearLayout)convertView.findViewById(R.id.problem_layout_item);
			holder.problemText = (TextView) convertView
					.findViewById(R.id.problem_item_text_desc);
			holder.problemTypeText = (TextView) convertView
					.findViewById(R.id.problem_item_text_fixtype);
			holder.typeText = (TextView) convertView
					.findViewById(R.id.problem_item_text_type);
			holder.requieText = (TextView) convertView
					.findViewById(R.id.problem_item_text_fix);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (null == list || position > list.size()) {
			return convertView;
		}
//		if(getCount()==1){
//			holder.problemLayout.setBackgroundResource(R.drawable.preference_one_item);
//		}
//		else{
//			if(position==0){
//				holder.problemLayout.setBackgroundResource(R.drawable.preference_first_item);
//			}
//			else if(position==getCount()-1){
//				holder.problemLayout.setBackgroundResource(R.drawable.preference_last_item);
//			}
//			else{
//				holder.problemLayout.setBackgroundResource(R.drawable.preference_item);
//			}
//		}
		Problem problem = list.get(position);
		holder.problemText.setText(TextHelper.getFliteStr(problem.problemDescn));
		holder.problemTypeText.setText(TextHelper.getFliteStr(problem.problemTypeValue));
		holder.typeText.setText(TextHelper.getFliteStr(problem.fixTypeValue));
		holder.requieText.setText(TextHelper.getFliteStr(problem.fixDescn));
		return convertView;
	}

}